﻿using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Models.Entitys;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Repository;
using RndMaterialDisposalService.Services.Interface.MaterialDisposal;
using Microsoft.AspNetCore.Mvc;
using VertivWorkflowCommonService;

namespace RndMaterialDisposalService.Services.MaterialDisposal
{
    /// <summary>
    /// 
    /// </summary>
    [NAutowired]
    public class OrgConfigurationService : IOrgConfigurationService
    {

        private readonly ILogger<OrgConfigurationService> logger;
        private readonly AssetsManagementContext dbContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="assetsManagementContext"></param>
        public OrgConfigurationService(ILogger<OrgConfigurationService> logger, AssetsManagementContext assetsManagementContext)
        {
            this.logger = logger;
            dbContext = assetsManagementContext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgConfiguration"></param>
        /// <returns></returns>
        public async Task<int> AddOrgConfiguration(OrgConfigurationDto orgConfiguration)
        {
            orgConfiguration.SysID = Guid.NewGuid();
            await dbContext.OrgConfiguration.AddAsync(orgConfiguration);
            return await dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysId"></param>
        /// <returns></returns>
        public async Task<int> DeleteOrgConfiguration(Guid sysId)
        {
            var model = await dbContext.OrgConfiguration.FirstOrDefaultAsync(p => p.SysID == sysId);
            dbContext.OrgConfiguration.Remove(model);
            return await dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgManageReq"></param>
        /// <returns></returns>
        public async Task<(List<OrgConfigurationDto> list, int count)> GetOrgConfigurationList(OrgManagementReq orgManageReq)
        {
            var data = dbContext.OrgConfiguration.Where(p => 1 == 1);
            if (!string.IsNullOrEmpty(orgManageReq.OrgName)) {
                data = data.Where(p => !string.IsNullOrEmpty(p.OrgName) && p.OrgName.Contains(orgManageReq.OrgName));
            }
            var count = await data.CountAsync();
            var list = await data.Skip((orgManageReq.PageIndex - 1) * orgManageReq.PageSize).Take(orgManageReq.PageSize).ToListAsync();
            
            return (list, count);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public async Task<(List<OrgConfigurationDto> list, int count)> GetOrgListByKeyword(string keyword, int PageIndex, int PageSize)
        {
            var data = dbContext.OrgConfiguration.Where(p => 1 == 1);
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(p => (!string.IsNullOrEmpty(p.OrgName) && p.OrgName.Contains(keyword) ));
            }
            var count = await data.CountAsync();
            var list = await data.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToListAsync();

            return (list, count);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgConfiguration"></param>
        /// <returns></returns>
        public async Task<int> UpdateOrgConfiguration(OrgConfigurationDto orgConfiguration)
        {
            var model = await dbContext.OrgConfiguration.FirstOrDefaultAsync(p => p.SysID == orgConfiguration.SysID);
            model.Merge(orgConfiguration);
            return await dbContext.SaveChangesAsync();
        }
    }
}
